package com.alibaba.nacos.config.server.jpa.service;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.config.server.model.ConfigAdvanceInfo;
import com.alibaba.nacos.config.server.model.ConfigAllInfo;
import com.alibaba.nacos.config.server.model.ConfigHistoryInfo;
import com.alibaba.nacos.config.server.model.ConfigInfo;
import com.alibaba.nacos.config.server.model.ConfigInfo4Beta;
import com.alibaba.nacos.config.server.model.ConfigInfo4Tag;
import com.alibaba.nacos.config.server.model.ConfigInfoAggr;
import com.alibaba.nacos.config.server.model.ConfigInfoBase;
import com.alibaba.nacos.config.server.model.ConfigInfoChanged;
import com.alibaba.nacos.config.server.model.ConfigInfoTagWrapper;
import com.alibaba.nacos.config.server.model.ConfigKey;
import com.alibaba.nacos.config.server.model.Page;
import com.alibaba.nacos.config.server.model.SameConfigPolicy;
import com.alibaba.nacos.config.server.model.SubInfo;
import com.alibaba.nacos.config.server.model.TenantInfo;
import com.alibaba.nacos.config.server.service.PersistService;

/**
 * The type of nacos-all.
 *
 * @author 问道于盲
 * @date 2020-03-08
 */
public interface IPersistService {

    boolean addAggrConfigInfo(final String dataId, final String group, String tenant, final String datumId,
                              String appName, final String content);

    void addConfigInfo(final String srcIp, final String srcUser, final ConfigInfo configInfo,
                      final Timestamp time, final Map<String, Object> configAdvanceInfo, final boolean notify);

    void addConfigInfo4Beta(ConfigInfo configInfo, String betaIps,
                            String srcIp, String srcUser, Timestamp time, boolean notify);


    void addConfigInfo4Tag(ConfigInfo configInfo, String tag, String srcIp, String srcUser, Timestamp time,
                           boolean notify);

    void addConfiTagRelationAtomic(long configId, String tagName, String dataId, String group, String tenant);

    void addConfiTagsRelationAtomic(long configId, String configTags, String dataId, String group,
                                    String tenant);

    int aggrConfigInfoCount(String dataId, String group, String tenant);

    int aggrConfigInfoCountIn(String dataId, String group, String tenant, List<String> datumIds);

    int aggrConfigInfoCountNotIn(String dataId, String group, String tenant, List<String> datumIds);

    Map<String, Object> batchInsertOrUpdate(List<ConfigAllInfo> configInfoList, String srcUser, String srcIp,
                                            Map<String, Object> configAdvanceInfo, Timestamp time, boolean notify, SameConfigPolicy policy) throws NacosException;

    boolean batchPublishAggr(final String dataId, final String group, final String tenant,
                             final Map<String, String> datumMap, final String appName);

    boolean batchRemoveAggr(final String dataId, final String group, final String tenant,
                            final List<String> datumList);

    int configInfoBetaCount();

    int configInfoCount();

    int configInfoCount(String tenant);

    int configInfoTagCount();

    ConfigHistoryInfo detailConfigHistory(Long nid);

    List<ConfigInfoChanged> findAllAggrGroup();


    Page<ConfigInfo> findAllConfigInfo(final int pageNo, final int pageSize, final String tenant);

    List<ConfigAllInfo> findAllConfigInfo4Export(final String dataId, final String group, final String tenant,
                                                 final String appName, final List<Long> ids);

    Page<ConfigInfoBase> findAllConfigInfoBase(final int pageNo,
                                               final int pageSize);

    Page<PersistService.ConfigInfoBetaWrapper> findAllConfigInfoBetaForDumpAll(
        final int pageNo, final int pageSize);

    Page<PersistService.ConfigInfoWrapper> findAllConfigInfoForDumpAll(
        final int pageNo, final int pageSize);

    Page<PersistService.ConfigInfoWrapper> findAllConfigInfoFragment(final long lastMaxId, final int pageSize);

    Page<ConfigInfoTagWrapper> findAllConfigInfoTagForDumpAll(
        final int pageNo, final int pageSize);

    Page<ConfigKey> findAllConfigKey(final int pageNo, final int pageSize, final String tenant);

    List<ConfigInfo> findAllDataIdAndGroup();

    Page<PersistService.ConfigInfoWrapper> findChangeConfig(final String dataId, final String group, final String tenant,
                                             final String appName, final Timestamp startTime,
                                             final Timestamp endTime, final int pageNo,
                                             final int pageSize, final long lastMaxId);

    List<PersistService.ConfigInfoWrapper> findChangeConfig(final Timestamp startTime,
                                             final Timestamp endTime);

    ConfigAdvanceInfo findConfigAdvanceInfo(final String dataId, final String group, final String tenant);

    ConfigAllInfo findConfigAllInfo(final String dataId, final String group, final String tenant);

    Page<ConfigHistoryInfo> findConfigHistory(String dataId, String group, String tenant, int pageNo,
                                              int pageSize);

    int findConfigHistoryCountByTime(final Timestamp startTime);

    ConfigInfo findConfigInfo(long id);

    ConfigInfo findConfigInfo(final String dataId, final String group, final String tenant);

    ConfigInfo4Beta findConfigInfo4Beta(final String dataId, final String group, final String tenant);

    Page<ConfigInfo> findConfigInfo4Page(final int pageNo, final int pageSize, final String dataId,
                                         final String group,
                                         final String tenant, final Map<String, Object> configAdvanceInfo);

    ConfigInfo4Tag findConfigInfo4Tag(final String dataId, final String group, final String tenant,
                                      final String tag);

    ConfigInfo findConfigInfoAdvanceInfo(final String dataId, final String group, final String tenant,
                                         final Map<String, Object> configAdvanceInfo);

    List<ConfigInfoAggr> findConfigInfoAggr(String dataId, String group, String tenant);

    Page<ConfigInfoAggr> findConfigInfoAggrByPage(String dataId, String group, String tenant, final int pageNo,
                                                  final int pageSize);

    Page<ConfigInfoAggr> findConfigInfoAggrLike(final int pageNo, final int pageSize, ConfigKey[] configKeys,
                                                boolean blacklist);

    ConfigInfo findConfigInfoApp(final String dataId, final String group, final String tenant,
                                 final String appName);

    ConfigInfoBase findConfigInfoBase(final String dataId, final String group);

    Page<ConfigInfoBase> findConfigInfoBaseByDataId(final int pageNo,
                                                    final int pageSize, final String dataId);

    Page<ConfigInfoBase> findConfigInfoBaseByGroup(final int pageNo,
                                                   final int pageSize, final String group);

    Page<ConfigInfoBase> findConfigInfoBaseLike(final int pageNo,
                                                final int pageSize, final String dataId, final String group,
                                                final String content) throws IOException;

    Page<ConfigInfo> findConfigInfoByAdvance(final int pageNo,
                                             final int pageSize, final String tenant,
                                             final Map<String, Object> configAdvanceInfo);


    Page<ConfigInfo> findConfigInfoByApp(final int pageNo,
                                         final int pageSize, final String tenant, final String appName);

    List<ConfigInfo> findConfigInfoByBatch(final List<String> dataIds,
                                           final String group, final String tenant, int subQueryLimit);

    Page<ConfigInfo> findConfigInfoByDataId(final int pageNo, final int pageSize, final String dataId,
                                            final String tenant);

    Page<ConfigInfo> findConfigInfoByDataIdAndAdvance(final int pageNo, final int pageSize, final String dataId,
                                                      final String tenant,
                                                      final Map<String, Object> configAdvanceInfo);

    Page<ConfigInfo> findConfigInfoByDataIdAndApp(final int pageNo, final int pageSize, final String dataId,
                                                  final String tenant, final String appName);

    Page<ConfigInfo> findConfigInfoByGroup(final int pageNo, final int pageSize, final String group,
                                           final String tenant);

    Page<ConfigInfo> findConfigInfoByGroupAndAdvance(final int pageNo,
                                                     final int pageSize, final String group, final String tenant,
                                                     final Map<String, Object> configAdvanceInfo);

    Page<ConfigInfo> findConfigInfoByGroupAndApp(final int pageNo,
                                                 final int pageSize, final String group, final String tenant,
                                                 final String appName);

    Page<ConfigInfo> findConfigInfoLike(final int pageNo,
                                        final int pageSize, final ConfigKey[] configKeys,
                                        final boolean blacklist);

    Page<ConfigInfo> findConfigInfoLike(final int pageNo, final int pageSize, final String dataId,
                                        final String group, final String tenant, final String appName,
                                        final String content);

    Page<ConfigInfo> findConfigInfoLike4Page(final int pageNo, final int pageSize, final String dataId,
                                             final String group, final String tenant,
                                             final Map<String, Object> configAdvanceInfo);

    List<ConfigInfo> findConfigInfosByIds(final String ids);

    long findConfigMaxId();

    List<String> findDatumIdByContent(String dataId, String groupId,
                                      String content);

    List<ConfigInfo> findDeletedConfig(final Timestamp startTime,
                                       final Timestamp endTime);

    ConfigInfoAggr findSingleConfigInfoAggr(String dataId, String group, String tenant, String datumId);


    List<TenantInfo> findTenantByKp(String kp);

    TenantInfo findTenantByKp(String kp, String tenantId);

    List<String> getConfigTagsByTenant(String tenant);


    List<String> getGroupIdList(int page, int pageSize);

    List<String> getTenantIdList(int page, int pageSize);

    void insertOrUpdate(String srcIp, String srcUser, ConfigInfo configInfo, Timestamp time,
                        Map<String, Object> configAdvanceInfo);

    void insertOrUpdate(String srcIp, String srcUser, ConfigInfo configInfo, Timestamp time,
                        Map<String, Object> configAdvanceInfo, boolean notify);

    void insertOrUpdateBeta(final ConfigInfo configInfo, final String betaIps, final String srcIp,
                            final String srcUser, final Timestamp time, final boolean notify);

    void insertOrUpdateSub(SubInfo subInfo);

    void insertOrUpdateTag(final ConfigInfo configInfo, final String tag, final String srcIp,
                           final String srcUser, final Timestamp time, final boolean notify);

    void insertTenantInfoAtomic(String kp, String tenantId, String tenantName, String tenantDesc,
                                String createResoure, final long time);

    boolean isExistTable(String tableName);

    List<PersistService.ConfigInfoWrapper> listAllGroupKeyMd5();

    PersistService.ConfigInfoWrapper queryConfigInfo(final String dataId, final String group, final String tenant);

    void removeAggrConfigInfo(final String dataId, final String group, final String tenant);

    void removeConfigHistory(final Timestamp startTime, final int limitSize);

    void removeConfigInfo(final String dataId, final String group, final String tenant, final String srcIp,
                          final String srcUser);

    void removeConfigInfo4Beta(final String dataId, final String group, final String tenant);

    List<ConfigInfo> removeConfigInfoByIds(final List<Long> ids, final String srcIp, final String srcUser);

    void removeConfigInfoTag(final String dataId, final String group, final String tenant, final String tag,
                             final String srcIp,
                             final String srcUser);

    void removeSingleAggrConfigInfo(final String dataId,
                                    final String group, final String tenant, final String datumId);

    void removeTagByIdAtomic(long id);

    void removeTenantInfoAtomic(final String kp, final String tenantId);

    boolean replaceAggr(final String dataId, final String group, final String tenant,
                        final Map<String, String> datumMap, final String appName);

    List<String> selectTagByConfig(String dataId, String group, String tenant);

    int tenantInfoCountByTenantId(String tenantId);


    void updateConfigInfo(final ConfigInfo configInfo, final String srcIp, final String srcUser,
                          final Timestamp time, final Map<String, Object> configAdvanceInfo,
                          final boolean notify);


    void updateConfigInfo4Beta(ConfigInfo configInfo, String srcIp, String srcUser, Timestamp time,
                               boolean notify);

    void updateConfigInfo4Tag(ConfigInfo configInfo, String tag, String srcIp, String srcUser, Timestamp time,
                              boolean notify);

    void updateMd5(String dataId, String group, String tenant, String md5, Timestamp lastTime);

    void updateTenantNameAtomic(String kp, String tenantId, String tenantName, String tenantDesc);



}
